﻿using System;
using System.IO;
using System.Windows;
using System.Drawing;
using System.Windows.Media;
using System.Drawing.Imaging;
using System.Windows.Media.Imaging;

namespace BassTutor
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }

    public static class Extensions{
        /// <summary>
        /// Statndard exception handline - shows MB with exception message and stack trace
        /// </summary>
        /// <param name="e">the exception</param>
        public static void Handle(this Exception e)
        {
            MessageBox.Show(e.Message + "\r\n\r\n" + e.StackTrace, "Exception cought!");
        }

        /// <summary>
        /// Gets ImageSource from Bitmap
        /// </summary>
        /// <param name="bmp">the bitmap</param>
        /// <returns></returns>
        public static ImageSource ToImageSource(this Bitmap bitmap){
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Png);
                memory.Position = 0;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                return bitmapImage;
            }
        }
    }
}
